package com.sgcc.pda.mdrh.task.safe.ui;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import com.sgcc.pda.baselibrary.utils.LogUtil;
import com.sgcc.pda.lib_tools_database.DBMaster;
import com.sgcc.pda.mdrh.app.ZoomApplication;
import com.sgcc.pda.mdrh.task.safe.oss.OSSHelper;
import com.sgcc.pda.mdrh.task.safe.service.BaseCheckService;
import com.sgcc.pda.tools.SPUtils;
import com.tencent.smtt.sdk.QbSdk;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class SafeApplication extends ZoomApplication {

    private ServiceConnection connection;

    @Override
    public void onCreate() {
        super.onCreate();
        initX5WebView();
        closeAndroidPDialog();

        DBMaster.getSingleton().init(this);
        SPUtils.init(this);
        LogUtil.init(true);

        OSSHelper.getInstance().initOSS(this);

        // 绑定服务
        connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                LogUtil.i("绑定服务成功！");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                LogUtil.i("解绑服务成功！");
            }
        };

        bindService(
                new Intent(this, BaseCheckService.class),
                connection,
                BIND_AUTO_CREATE);
        //插件安装服务绑定
//        bindService(new Intent(this, PluginService.class), connection, BIND_AUTO_CREATE);
    }

    /**
     * 初始化x5
     */
    private void initX5WebView() {
        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

            @Override
            public void onViewInitFinished(boolean arg0) {
                Log.d("app", " onViewInitFinished is " + arg0);
            }

            @Override
            public void onCoreInitFinished() {
            }
        };
        //x5内核初始化接口
        QbSdk.initX5Environment(getApplicationContext(), cb);
    }

    /**
     * 去掉Android P上的Detected提醒弹窗
     */
    @SuppressWarnings("unchecked")
    @SuppressLint("PrivateApi")
    private void closeAndroidPDialog() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            try {
                @SuppressWarnings("rawtypes")
                Class aClass = Class.forName("android.content.pm.PackageParser$Package");
                @SuppressWarnings("rawtypes")
                Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
                declaredConstructor.setAccessible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                @SuppressWarnings("rawtypes")
                Class cls = Class.forName("android.app.ActivityThread");
                @SuppressLint("DiscouragedPrivateApi")
                Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
                declaredMethod.setAccessible(true);
                Object activityThread = declaredMethod.invoke(null);
                Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
                mHiddenApiWarningShown.setAccessible(true);
                mHiddenApiWarningShown.setBoolean(activityThread, true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        unbindService(connection);
    }

}
